﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LinkCard.Model;
using LinkCard.Dao;

namespace LinkCard.CustomerView
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            long dealid = 0;
            var did = Page.RouteData.Values["did"];
            if (did != null) dealid = long.Parse(did.ToString());

            var deal = new DealDao().GetById(dealid);
            if (deal == null)
                return;

            long parentcatid = 0;
            var parentcat = Page.RouteData.Values["pcid"];
            if (long.TryParse(parentcat.ToString(), out parentcatid))
            {
                Model.Category cat = new CategoryDao().GetById(parentcatid);
                if (cat != null)
                {
                    parentcatid = (long)cat.ParentID;
                    breadcrumsp.HRef = "/danh-muc-" + cat.CategoryID + "/m-2/" + LinkCard.Utils.Utilities.ConvertTextToUrl(cat.Name) + ".html"; //LinkCard.Utils.Utilities.GetProductDetailUrl(cat.CategoryID, cat.Name, cat.ParentID);
                    breadcrumsp.InnerHtml = HttpUtility.HtmlDecode(cat.Name + " &gt; ");
                }
            }

            dealmenuname.Text = HttpUtility.HtmlDecode(deal.Name);
            dealname.Text = HttpUtility.HtmlDecode(deal.Name);
            dealprice.Text = LinkCard.CustomerView.Common.Helper.FormatCurrencyVND(deal.Price);
            dealpoint.Text = deal.Point != null ? Convert.ToString(deal.Point) : "0";
            dealintroduce.InnerHtml = HttpUtility.HtmlDecode(deal.Detail);

            string taghtml = "";
            if (!string.IsNullOrEmpty(deal.Tags))
            {
                List<string> strarr = new List<string>();
                foreach (string s in deal.Tags.Split(','))
                {
                    strarr.Add("<a href='/tu-khoa/tag=" + s + ".html'>" + s + "</a>");
                }
                taghtml = string.Join(", ", strarr);
            }
            else
            {
                tagbox.Visible = false;
            }
            lsttag.InnerHtml = taghtml;

            if (deal.SupplierID != null)
            {
                Supplier supplier = new SupplierDao().GetById((long)deal.SupplierID);
                if (supplier != null)
                {
                    suppliername.Text = HttpUtility.HtmlDecode(supplier.Name);
                    //parnerintroduce.InnerHtml=HttpUtility.HtmlDecode(supplier.)
                    supplieraddress.InnerHtml = HttpUtility.HtmlDecode(supplier.Address);
                    supplierlink.HRef = "/doi-tac-" + supplier.SupplierID + "/m-2/" + LinkCard.Utils.Utilities.ConvertTextToUrl(supplier.Name) + ".html";
                }
            }

            if (!string.IsNullOrEmpty(deal.Image))
            {
                //<img alt="" src="http://i.nokia.com/image/view/-/1674238/highRes/3/-/DKR-png.png" />

                var imgsource = Utils.Utilities.GetProductImage(deal.Image);
                rpalbum.DataSource = imgsource;
                rpalbum.DataBind();

                rplistimg.DataSource = imgsource;
                rplistimg.DataBind();
            }

            if (deal.CategoryID != null)
            {
                var sanphamungloai = new DealDao().GetSameDeal(deal.DealID, (long)deal.CategoryID);

                var source = (from item in sanphamungloai
                              select new
                                         {
                                             item.DealID,
                                             item.Name,
                                             Price = LinkCard.CustomerView.Common.Helper.FormatCurrencyVND(item.Price),
                                             ImageUrl = Utils.Utilities.GetProductImage(item.Image).FirstOrDefault(),
                                             ParentCategoryId = parentcatid
                                         }).ToList();

                rpsamedeal.DataSource = source;
            }
            rpsamedeal.DataBind();

            Page.Title = deal.Name + " - Quyền lợi khách hàng - LinkCard";
            Page.MetaDescription = deal.Name + " - " + deal.Description + " - Quyền lợi khách hàng - LinkCard";
        }
    }
}